package com.leapcloud.crm.core.generate;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultTransactionProvider;

public class JooqFactory {
  public final static String DATABASE="vtiger";
  // 表示定义数据库的用户名
  private final static String USERNAME = "root";
  // 定义数据库的密码
  private final static String PASSWORD = "123456";
  // 定义数据库的驱动信息
  private final static String DRIVER = "com.mysql.jdbc.Driver";
  // 定义访问数据库的地址
  private final static String URL = "jdbc:mysql://127.0.0.1:3306/"+DATABASE+"?characterEncoding=utf8&useSSL=false";
  public DSLContext getDSLContext() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(URL);
    dataSource.setUsername(USERNAME);
    dataSource.setPassword(PASSWORD);
    dataSource.setMaxActive(100);
    dataSource.setInitialSize(1);
    dataSource.setMinIdle(1);
    dataSource.setMaxWait(20_000);
    dataSource.setTestOnBorrow(true);
    dataSource.setTestWhileIdle(true);
    dataSource.setMinEvictableIdleTimeMillis(1000*60*10);
    dataSource.setTimeBetweenEvictionRunsMillis(60*1000);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setMaxPoolPreparedStatementPerConnectionSize(50);
    dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker());
    ConnectionProvider connectionProvider =  new DataSourceConnectionProvider(dataSource);
    TransactionProvider transactionProvider = new DefaultTransactionProvider(connectionProvider, false);
    Configuration configuration = new DefaultConfiguration()
        .set(connectionProvider)
        .set(transactionProvider)
        .set(SQLDialect.MYSQL);
    return DSL.using(configuration);
  }

  public void init(){
    DSLContext dslContext=getDSLContext();
  }

}